#include "NeuronTargetDialog.h"
#include "ui_NeuronTargetDialog.h"
#include "math.h"
#include "QImage"

NeuronTargetDialog::NeuronTargetDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NeuronTargetDialog)
{
    ui->setupUi(this);
}

NeuronTargetDialog::~NeuronTargetDialog()
{
    //ui->tableWidget_spectre->clearContents();
    delete ui;
}

void NeuronTargetDialog::setResults(bool result, QString state)
{
    ui->textBrowser->setText(state);
    QString result_msg;
    switch(result){
    case 1:
        result_msg = "совпадение";
        break;
    case 0:
        result_msg = "нет совпадения";
        break;
    }
    ui->label_result_status->setText(result_msg);
}

void NeuronTargetDialog::drawSpectre(float w0, float* pW, int N_inputs, bool isLine)
{
    float min = w0;
    float max = w0;
    int img_size = ui->label_spectre->height();
    for(int i = 0; i < N_inputs; i++)
    {
        if(min > *(pW+i)) min = *(pW+i);
        if(max < *(pW+i)) max = *(pW+i);
    }

    float disp_step = qMax(qAbs(min),qAbs(max))/255.0;

    QImage image(img_size, img_size, QImage::Format_ARGB32);

    if(!isLine)
    {
        int size = sqrt(N_inputs);

        int step = img_size/size;
        for(int i = 0; i < N_inputs; i++)
        {
            if(*(pW+i) > 0)
            {
                int value = (*(pW+i))/disp_step;
                for(int j = 0; j < step*step; j++)
                    image.setPixel(i%size*step + j%step, i/size*step + j/step, qRgba(255, 0, 0, value));
            }
            else
            {
                int value = *(pW+i)*(-1)/disp_step;
                for(int j = 0; j < step*step; j++)
                    image.setPixel(i%size*step + j%step, i/size*step + j/step, qRgba(0, 0, 255, value));
            }
        }
    }
    else
    {
        int size = N_inputs;

        int step = img_size/size;
        for(int i = 0; i < N_inputs; i++)
        {
            if(*(pW+i) > 0)
            {
                int value = (*(pW+i))/disp_step;
                for(int j = 0; j < step*step; j++)
                    image.setPixel(i%size*step + j%step, i/size*step + j/step, qRgba(255, 0, 0, value));
            }
            else
            {
                int value = *(pW+i)*(-1)/disp_step;
                for(int j = 0; j < step*step; j++)
                    image.setPixel(i%size*step + j%step, i/size*step + j/step, qRgba(0, 0, 255, value));
            }
        }
    }
    ui->label_spectre->setPixmap(QPixmap::fromImage(image));
}
